<?php
class Admin extends AdminAction{
	function index(){
		$admin = getSession('adminuser');
		if(empty($admin)){
			template('index',$this->data);
		}else{
			header('location:/admin.php/admin/console');
		}
	}
	
	function login(){
		
		$loginname = P("loginname");
		$loginpass = P("loginpass");
		
		$db=$this->getDB();
		$user = $db->get_assoc("select * from manager where loginname='$loginname'");
		$message = array();
		if(empty($user)){
			$message = setMessage(false,'用户不存在！',$message);
		}else{
			if($user['loginpass'] == $loginpass){
				setSession('adminuser', $user);
				setSession('editlang', 'cn');
				$message = setMessage(true,'用户登入成功！',$message);
			}else{
				$message = setMessage(false,'用户密码错误！',$message);
			}
		}
		echo json_encode($message);
	}
	
	function console(){
		$admin = getSession('adminuser');
		if(!empty($admin)){
			template('console',$this->data);
		}else{
			header('location:/admin.php');
		}
	}
	
	function out(){
		setSession("adminuser", null);
		header('location:/admin.php');
	}
	
	function changelang($lang){
		setSession('editlang', $lang);
		message('编辑语言切换成功！', '/admin.php/admin/console');
	}
	
	function main(){
		$db = $this->getDB();
		$db_version = $db->get_assoc("select VERSION() version");
		$dbname = $db->getParam('db_database');
		$db_size = $db->get_assoc("SELECT table_schema , sum( data_length + index_length ) / 1024 / 1024 dbsize FROM information_schema.TABLES where table_schema = '$dbname'  GROUP BY table_schema ");
		$this->data['dbversion'] = $db_version['version'];
		$this->data['dbsize']=$db_size['dbsize'];
		
		template('main',$this->data);
	}
	
	function showupdatepwd(){
		template('updatepwd',$this->data);
	}
	
	function updatepwddo(){
		$db = $this->getDB();
		$id = P('id');
		$newpass = P('newpass');
		
		$db->query("update manager set loginpass = '$newpass' where id = $id");
		message('密码修改成功！', '/admin.php/admin/showupdatepwd');
	}
	
} 